package com.caifeng.myapplication.ViewPager;

import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.caifeng.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class DemoVP extends AppCompatActivity {

    private ViewPager viewPager;
    private RadioGroup rg;


    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_main);

        viewPager = findViewById(R.id.vp);
        rg = findViewById(R.id.rg);
        viewPager.setAdapter(new VPAdapter(getSupportFragmentManager(), getFragment()));

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.one:
                        viewPager.setCurrentItem(0);

                        break;
                    case R.id.two:
                        viewPager.setCurrentItem(1);
                        break;
                }
            }
        });
    }

    public List<Fragment> getFragment() {
        fragments = new ArrayList<>();
        fragments.add(new OneVP());
        fragments.add(new TwoVP());


        return fragments;
    }


}
